package com.vdong.trade.trading.service.dubbo;

import com.alibaba.fastjson.JSON;
import com.vdong.trade.trading.common.exception.ValidateException;
import com.vdong.trade.trading.entity.dto.param.PayConfigParamDTO;
import com.vdong.trade.trading.entity.dto.result.PayConfigResultDTO;
import com.vdong.trade.trading.entity.mo.PayConfigMO;
import com.vdong.trade.trading.facade.PayConfigFacade;
import com.vdong.trade.trading.service.PayConfigService;
import lombok.extern.slf4j.Slf4j;
import org.lamb.framework.common.remote.FacadeResult;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@com.alibaba.dubbo.config.annotation.Service
public class PayConfigFacadeImpl implements PayConfigFacade {

    @Autowired
    private PayConfigService payConfigService;


    @Override
    public FacadeResult<String> insert(String request) throws Exception {
        log.info("新增商户配置请求参数:[{}]", request);
        try {
            if (StringUtil.hasBlank(request)) {
                return FacadeResult.buildFail("请求参数不能为空!");
            }
            PayConfigParamDTO payConfigDTO = JSON.parseObject(request, PayConfigParamDTO.class);
            payConfigService.insert(payConfigDTO);
            return FacadeResult.buildSucc();
        } catch (Exception e) {
            log.error("");
            return FacadeResult.buildFail("新增商户配置数据异常!");
        }
    }

    @Override
    public FacadeResult<String> update(String request) throws Exception {
        log.info("更新商户配置请求参数:[{}]", request);
        try {
            if (StringUtil.hasBlank(request)) {
                return FacadeResult.buildFail("请求参数不能为空!");
            }
            PayConfigParamDTO payConfigDTO = JSON.parseObject(request, PayConfigParamDTO.class);
            Integer count = payConfigService.update(payConfigDTO);
            if (count < 1) {
                return FacadeResult.buildFail("更新商户配置失败!");
            }
        } catch (ValidateException e) {
            log.info("更新商户配置失败:", e);
            return FacadeResult.buildFail(e.getErrorMessage());
        } catch (Exception e) {
            log.info("更新商户配置失败:", e);
            return FacadeResult.buildFail("更新商户配置数据异常!");
        }

        return FacadeResult.buildSucc();
    }

    @Override
    public FacadeResult<String> selectPayConfigList(String request) throws Exception {
        PayConfigParamDTO paramDTO = new PayConfigParamDTO();
        List<PayConfigMO> moList = payConfigService.selectPayConfigList(paramDTO);

        List<PayConfigResultDTO> list = new ArrayList<>();
        moList.forEach(mo -> {
                    PayConfigResultDTO resultDto = new PayConfigResultDTO();
                    CopyUtil.copyObject(mo, resultDto);
                    list.add(resultDto);
                }
        );

        return FacadeResult.buildSucc(JSON.toJSONString(list));
    }


}
